﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
namespace ToaThuoc_PAK.Handler
{
    /// <summary>
    /// Summary description for XML_LoadDanhMucThuoc
    /// </summary>
    public class XML_LoadDanhMucThuoc : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string DBName1 = "";
            string DBName2 = "";
            string MaND = Factory.emptyNull(context.Session["mand"]);
            string prefix = Factory.emptyNull(context.Request.QueryString["prefix"]);
            string NguonThuoc = Factory.emptyNull(context.Request.QueryString["NguonThuoc"]);
            DAL_Thuoc dal_Thuoc = new DAL_Thuoc();
            string xmlReturn = "<Root>" + "\n";
            /*---lay thuoc tu hai kho
                @DBName1: KhoBH	| @DBName2: KhoDV
            ---lay thuoc tu 1 kho
                @DBName1: ''		| @DBName2: KhoDV
               hoặc 
                @DBName1: 'KhoBH'	| @DBName2: ''
            */
            switch (NguonThuoc)
            {
                case "BHYT":
                    DBName1 = "KhoBH";
                    DBName2 = "";
                    break;
                case "DV":
                    DBName1 = "";
                    DBName2 = "KhoDV";
                    break;
                default:
                    DBName1 = "KhoBH";
                    DBName2 = "";
                    break;
            }
            DataTable dt = dal_Thuoc.LayDanhSachThuoc(DBName1, DBName2, MaND);
            if (dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow row in dt.Rows)
                {
                    xmlReturn += "<Thuoc ID=\"" + Factory.emptyNull(row["ID"]) + "\"" + "\n" +
                        "                TenThuoc=\"" + Factory.emptyNull(row["tenthuoc"]) + "\"" + "\n" +
                        "                DonVi=\"" + Factory.emptyNull(row["DonVi"]) + "\"" + "\n" +
                        "                SoLuong=\"" + Factory.emptyNull(row["SoLuong"]) + "\"" + "\n" +
                        "                CachDung=\"" + Factory.emptyNull(row["CachDung"]) + "\"" + "\n" +
                        "                SoLanDung=\"" + Factory.emptyNull(row["SoLanDung"]) + "\"" + "\n" +
                        "                DonViDung=\"" + Factory.emptyNull(row["DonViDung"]) + "\"" + "\n" +
                        "                GhiChu=\"" + Factory.emptyNull(row["GhiChu"]) + "\"" + "\n" +
                        "                SoLuongCon=\"" + Factory.emptyNull(row["SoLuongCon"]) + "\"" + "\n" +
                        "                DonGia=\"" + Factory.emptyNull(row["DonGia"]) + "\"" + "\n" +
                        "                DBName=\"" + Factory.emptyNull(row["DBName"]) + "\"" + "\n" +
                        "                HoatChatChinh=\"" + Factory.emptyNull(row["HoatChatChinh"]) + "\"" + "\n" +
                        "                HoTriLieu=\"" + Factory.emptyNull(row["HoTriLieu"]) + "\"" + "\n" +
                        "                ></Thuoc>" + "\n"; ;
                }
            }
            xmlReturn += "</Root>";
            context.Response.ContentType = "text/xml";
            context.Response.Write(xmlReturn);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}